/**
 * 
 */
package cn.mxz.robots.province;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import net.sf.json.JSONObject;
import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 内蒙古
 * 排名：完成
 * 去年排名：完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-19
 */
public class InnerMongolia extends Exam implements IRank
{

	public static void main(String[] args)
	{
		InnerMongolia inner = new InnerMongolia(new Student("15150105112365", "123456", "", "", BEN_KE_LI_KE));
		System.out.println(inner.login(""));
	
	}

	public InnerMongolia(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://www1.nm.zsks.cn/gkcx/kscj.do", mSha1);
		loginRequest.addParams("ksh", mStudent.getStudentId());
		loginRequest.addParams("mm", mStudent.getStudentPwd());

		String loginHtml = curlPost(loginRequest);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("姓名"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("考生号错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("密码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_PASSWORD);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document resultDoc = Jsoup.parse(parseHtml);
		Elements trs = resultDoc.select("table").get(1).select("tr");

		JSONObject data = new JSONObject();
		data.put("exam_id", trs.get(1).select("td").get(0).text());
		data.put("name", trs.get(1).select("td").get(1).text());
		data.put("ranking", getRank(19+31, trs.get(1).select("td").get(8).text(), mStudent.getType()));
		data.put("province", "内蒙古");

		JSONObject score = new JSONObject();
		score.put("语文", trs.get(1).select("td").get(2).text());
		score.put("文数", trs.get(1).select("td").get(3).text());
		score.put("文综", trs.get(1).select("td").get(4).text());
		score.put("英语", trs.get(1).select("td").get(5).text());
		score.put("口语", trs.get(1).select("td").get(6).text());
		score.put("听力", trs.get(1).select("td").get(7).text());
		score.put("总分", trs.get(1).select("td").get(8).text());
		score.put("总分1", trs.get(1).select("td").get(9).text());
		data.put("score", score.toString());

		return data.toString();
	}
	
	@Override
	public void initRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(19 + 31);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2015/123950.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2015/123951.html"));
		// 文科 http://www.gxeduw.com/news/2015/123951.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 0;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			//String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");

			
			Integer ranking = pre + 1;
			
			Integer count = Integer.parseInt(leiji)-pre;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_WEN_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + count + "rank: " + ranking);
			System.out.println(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(19);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2014/107024.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2014/107024.html"));
		// 文科 http://www.gxeduw.com/news/2014/107025.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 0;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			//String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			String scoreString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			//String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			//String leiji = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(2).text(), "(\\d+)");

			if(scoreString.equals("")||leiji.equals(""))continue;
			Integer ranking = pre + 1;
			
			Integer count = Integer.parseInt(leiji)-pre;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_LI_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + count + "rank: " + ranking);
			System.out.println(rank);
		}
	}
}
